package com.zhuhjay.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhuhjay.commonutils.JwtUtils;
import com.zhuhjay.commonutils.MD5;
import com.zhuhjay.servicebase.exception.GuLiException;
import com.zhuhjay.ucenter.entity.Member;
import com.zhuhjay.ucenter.entity.vo.RegisterVo;
import com.zhuhjay.ucenter.mapper.MemberMapper;
import com.zhuhjay.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author ZhuHJay
 * @since 2022-09-08
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(Member member) {
        // 手机 密码不能为空
        if (StringUtils.isEmpty(member.getMobile()) ||
                StringUtils.isEmpty(member.getPassword())) {
            throw new GuLiException(20001, "手机号或密码不能为空");
        }
        // 判断手机 密码
        Member user = this.getOne(new LambdaQueryWrapper<Member>()
                .eq(Member::getMobile, member.getMobile())
                .eq(Member::getPassword, MD5.encrypt(member.getPassword())));
        if (ObjectUtils.isEmpty(user)) {
            throw new GuLiException(20001, "手机号或密码错误");
        }
        // 判断用户状态
        if (user.getIsDeleted()) {
            throw new GuLiException(20001, "用户已被删除");
        }
        if (user.getIsDisabled()) {
            throw new GuLiException(20001, "用户已被禁用");
        }
        // 生成token返回
        return JwtUtils.getJwtToken(user.getId(), user.getNickname());
    }

    @Override
    public void register(RegisterVo registerVo) {
        // 输入不能为空
        if (StringUtils.isEmpty(registerVo.getMobile()) ||
                StringUtils.isEmpty(registerVo.getNickname()) ||
                StringUtils.isEmpty(registerVo.getPassword()) ||
                StringUtils.isEmpty(registerVo.getCode())) {
            throw new GuLiException(20001, "注册申请失败");
        }
        // 获取验证码
        String code = redisTemplate.opsForValue().get(registerVo.getMobile());
        if (StringUtils.isEmpty(code)) {
            throw new GuLiException(20001, "验证码失效，请重新发送");
        }
        // 验证码验证
        if (!code.equals(registerVo.getCode())) {
            throw new GuLiException(20001, "验证码错误");
        }
        // 手机号不能重复
        if (this.count(new LambdaQueryWrapper<Member>()
                .eq(Member::getMobile, registerVo.getMobile())) > 0) {
            throw new GuLiException(20001, "手机号已被使用");
        }
        // 正式注册
        Member member = new Member();
        BeanUtils.copyProperties(registerVo, member);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/MQ7qUmCprK9am16M1Ia1Cs3RK0qiarRrl9y8gsssBjIZeS2GwKSrnq7ZYhmrzuzDwBxSMMAofrXeLic9IBlW4M3Q/132");
        // 密码加盐
        member.setPassword(MD5.encrypt(registerVo.getPassword()));
        this.save(member);
    }

    @Override
    public Member getInfoById(String id) {
        // 获取用户信息
        Member member = this.getById(id);
        // 判断用户状态
        if (member.getIsDeleted()) {
            throw new GuLiException(20001, "用户已被删除");
        }
        if (member.getIsDisabled()) {
            throw new GuLiException(20001, "用户已被禁用");
        }
        member.setPassword(null);
        return member;
    }

    @Override
    public Long getRegisterCountByDay(String day) {
        return this.baseMapper.getRegisterCount(day);
    }
}
